<template>
    <section>
        <!--工具条-->

        <div class="section-content clearfix">
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">
                    <!-- <el-form-item>
                        <el-input v-model="filters.name" placeholder="姓名"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" v-on:click="getUsers">查询</el-button>
                    </el-form-item> -->
                    <el-form-item>
                        <el-button :type="buttonColor" @click="getUserlists">关注列表</el-button>
                    </el-form-item>

                    <el-form-item>
                        <el-button :type="buttonColor" @click="getUserlist">已取消关注列表</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--列表-->

            <h2 v-if="status== 1" style="font-size: 20px;font-weight: bold;color:#475669">关注列表</h2>
            <h2 v-if="status== 0" style="font-size: 20px;font-weight: bold;color:#475669">已取消关注列表</h2>
            <el-table :data="query" highlight-current-row v-loading="listLoading" :header-row-style="{backgroundColor: titleColor}" @selection-change="selsChange"
                style="width: 100%;">
                <!-- <el-table-column type="selection" v-show="status== 1">
                </el-table-column> -->
                <el-table-column type="index" label="序号" width="80" :index="indexMethod">
                </el-table-column>
                <el-table-column align="center" label="设备名称">
                    <template scope="scope">
                        <span class="pb-name" @click="details(scope.$index, scope.row)">{{scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="deviceSn" label="网关SN">
                </el-table-column>
                <el-table-column align="center" prop="stateText" label="状态">
                </el-table-column>
                <!-- <el-table-column align="center" prop="terminalUserName" label="终端用户">
                </el-table-column> -->
                <el-table-column align="center" label="排序" v-if="status==1">
                    <template scope="scope">
                        <el-button size="mini"  
                            @click="handleup(scope.$index, scope.row)" title="置顶" plain><img class="up-img" src="../../assets/8a542d43248d7f6a36e39ef0727858f.png"></el-button>
                        <!-- <el-button size="mini" icon="el-icon-arrow-up" type="info"
                            @click="gettop1(scope.$index, scope.row)" title="上移" plain></el-button> -->
                        <el-button size="mini"  
                            @click="getdown(scope.$index, scope.row)" title="置底" plain><img class="up-img" src="../../assets/761bc582cd35f1b90c28d8f2a080854.png"></el-button>
                      
                        <!-- <el-button size="mini" icon="el-icon-arrow-up" type="info"
                            @click="gettop1(scope.$index, scope.row)" title="上移" plain></el-button>
                    
                        <el-button size="mini" icon="el-icon-arrow-down" type="info"
                            @click="getdown1(scope.$index, scope.row)" title="下移" plain></el-button> -->
                    </template>
                </el-table-column>

                <el-table-column align="center" label="操作">
                    <template scope="scope">

                        <i title="关注" v-if="status==0" @click="collect(scope.$index, scope.row)" type="warning"
                            icon="el-icon-star-off" class="pb-icon icon-attention"></i>
                        <!-- <i class="pb-icon icon-zhiding" v-if="status==1" @click="handleup(scope.$index, scope.row)"
                            title="设备置顶" circle></i> -->
                        <!-- <i @click="handleEdit(scope.$index, scope.row)" title="编辑" icon="el-icon-edit" circle
                            class="pb-icon icon-edit"></i> -->
                        <i v-if="status==1" @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete" circle
                            title="删除" class="pb-icon icon-delete"></i>
                        <!-- <el-button type="danger" v-if="status==1" size="small" @click="handleDel(scope.$index, scope.row)"
                            icon="el-icon-delete" circle title="取消关注"></el-button> -->
                    </template>
                </el-table-column>

            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <!-- <el-button v-if="status==1" type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除
                </el-button> -->
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10"
                    :total="total" style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" v-model="editFormVisible" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="排序值" prop="name">
                        <el-input v-model="priority" type="number" min="0" auto-complete="off"></el-input>
                    </el-form-item>


                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="handby">提交</el-button>
                </div>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="类型名称" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="是否默认">
                        <el-radio-group v-model="addForm.isDefault">
                            <el-radio label="0" aria-checked="true">否</el-radio>
                            <el-radio label="1">是</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="remark" label="描述">
                        <el-input v-model="addForm.remark" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="属性">
                        <el-row :gutter="10" style="padding:10px" v-for="item in  addForm.deviceTypeAttrFormList">

                            <el-col :span="7">

                                <el-input v-model="item.attrId" auto-complete="off" placeholder="属性类型"></el-input>
                            </el-col>
                            <el-col :span="7">
                                <el-input v-model="item.attrValue" auto-complete="off" placeholder="属性值"></el-input>
                            </el-col>
                            <el-col :span="7">
                                <el-input v-model="item.attrRemark" auto-complete="off" placeholder="属性描述"></el-input>
                            </el-col>
                        </el-row>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">

                    <el-button type="success" @click.native="natureadd">新增</el-button>
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        device_favorites_query,
        device_favorites_cancel,
        device_favorites_top,
        device_favorites_add,
        device_type_delete,
        device_favorites_orderby
    } from '../../api/api';

    export default {
        data() {
            return {
                priority: '',
                status: 1,
                filters: {
                    name: ''
                },
                nature: 1,
                query: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列
                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }]
                },
                //编辑界面数据
                editForm: {
                    id: '',
                    name: '',
                    isDefault: '',
                    remark: '',
                    deviceTypeAttrFormList: [{
                        'attrId': '1',
                        'attrValue': '2',
                        'attrRemark': '123'
                    }]
                },

                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    path: [{
                            required: true,
                            message: '请输入path',
                            trigger: 'blur'
                        }

                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    isDefault: '',
                    remark: '',
                    deviceTypeAttrFormList: [{
                        'attrId': '1',
                        'attrValue': '2',
                        'attrRemark': '123'
                    }]

                }

            }
        },
        computed:{
        titleColor(){
                    return this.$store.state.titleColor
                },
        buttonColor(){
                    return this.$store.state.buttonColor
                },
        },
        methods: {
            rank(s, d) {
                this.bodyList = [];

                let para = {
                    deviceId: d,
                    priority: s 
                }
                device_favorites_orderby(para).then((res) => {
                    this.begCode(res.code, res.message);
                    // this.total = res.data.totalCount;
                    this.getUsers()
                    //NProgress.done();
                });
            },
            gettop1(index, row) {
                let para = Object.assign({}, row);
                if (this.query[index - 1]) {
                    let num = row.priority;
                    row.priority = this.query[index - 1].priority;
                    this.query[index - 1].priority = num;
                    this.rank(row.priority, row.deviceId)
                }
            },
            getdown1(index, row) {
                let para = Object.assign({}, row);
                if (this.query[index + 1]) {
                    let num = row.priority;
                    row.priority = this.query[index + 1].priority;
                    this.query[index + 1].priority = num;
                    this.rank(row.priority, row.deviceId)
                }
            },
            gettop(index, row) {

                if (this.query[index - 1]) {
                    row.priority = this.query[0].priority - 1;
                    this.rank(row.priority, row.deviceId)
                }
                return false;

            },
            getdown(index, row) {
                // if (this.query[index + 1]) {
                //     row.priority = this.query[this.query.length - 1].priority + 1;
                //     this.rank(row.priority, row.deviceId)
                // }
                this.rank(9999, row.deviceId)
                return false;

            },
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            details: function (index, row) {
                sessionStorage.setItem('facility_details', row.deviceId)
                this.$router.push({
                    path: '/devices/deviceDatail'
                })
            },
            natureadd() {
                let s = {
                    'attrId': '',
                    'attrValue': '',
                    'attrRemark': ''
                }
                this.addForm.deviceTypeAttrFormList.push(s)
                this.editForm.deviceTypeAttrFormList.push(s)
                // alert(this.nature);
            },
            //关注
            collect: function (index, row) {

                let para = {
                    deviceId: row.deviceId
                }
                device_favorites_add(para).then((res) => {
                     this.begCode(res.code,res.message);

                    this.listLoading = false;
                    //NProgress.done();
                    this.$message({
                        message: '关注设备成功',
                        type: 'success'
                    });
                    this.getUsers();
                });

            },
            //性别显示转换
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取菜单列表
            getUserlists() {
                this.status = 1;
                this.handleCurrentChange(1)
            },
            getUserlist() {
                this.status = 0;
                this.handleCurrentChange(1)
            },
            getUsers() {
                if (this.menubt.indexOf('/device_favorites/query') == -1) {
                    this.$message({
                        message: '没有查看关注列表的权限',
                        type: 'warning'
                    });
                    return false;
                }
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    name: this.filters.name,
                    status: this.status
                };
                this.listLoading = true;
                //NProgress.start();
                device_favorites_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.total = res.data.totalCount;
                    this.query = res.data.list;
                    this.listLoading = false;
                    this.page = res.data.pageNo;
                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认取消关注吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        deviceIdList: [row.deviceId]
                    };
                    device_favorites_cancel(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        }
                    });
                }).catch(() => {

                });
            },
            //关注设备置顶
            handleup: function (index, row) {
                let para = {
                    deviceId: row.deviceId
                };
                device_favorites_top(para).then((res) => {
                     this.begCode(res.code,res.message);
                    if (res.code == 0) {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '设备已置顶成功',
                            type: 'success'
                        });
                        this.getUsers();


                    }
                });
            },
            //显示编辑界面
            handby() {
                let para = {
                    deviceId: this.editForm.deviceId,
                    priority: this.priority
                }
                device_favorites_orderby(para).then((res) => {

                    this.editFormVisible = false;
                    this.begCode(res.code, res.message);
                    if (res.code == 0) {
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                        this.getUsers();

                    }
                });
            },
            handleEdit: function (index, row) {
                this.editForm = row;
                this.priority = row.priority;
                this.editFormVisible = true;
                // let para = {
                //     id: row.id
                // };
                // device_type_get(para).then((res) => {
                //      this.begCode(res.code,res.message);
                //     if (res.code == 0) {

                //         this.editFormVisible = true;
                //         this.editForm = Object.assign({}, res.data);
                //         this.editForm.isDefault = this.editForm.isDefault.toString();

                //     }
                // });

                // this.editForm.deviceTypeAttrFormList= [{'attrId':'','attrValue':'','attrRemark':''}];
            },
            //显示新增界面
            handleAdd: function () {
                this.nature = 1;
                this.addFormVisible = true;
                this.addForm = {

                    name: '',
                    isDefault: '',
                    remark: '',
                    deviceTypeAttrFormList: [{
                        'attrId': '',
                        'attrValue': '',
                        'attrRemark': ''
                    }]
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);

                            alarms_put_user(para).then((res) => {
                                 this.begCode(res.code,res.message);

                                if (res.code == 0) {
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);

                            device_type_post(para).then((res) => {
                                 this.begCode(res.code,res.message);
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.deviceId).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        deviceIdList: ids.split(',')
                    };
                    device_favorites_cancel(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.menubt = localStorage.getItem('menubt');
            this.getUsers();
        }
    }
</script>

<style scoped>
  .up-img{
        width: 10px;
        height: 10px;
        padding: 0 1px;
    }
</style>